package com.xiaohu.myspace.database.daoimple;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.xiaohu.myspace.database.MyHibernateDaoSupport;
import com.xiaohu.myspace.database.dao.MessageInfoDao;
import com.xiaohu.myspace.database.entity.MessageInfo;
import com.xiaohu.myspace.vo.MessageInfoVo;

public class MessageInfoDaoImple extends MyHibernateDaoSupport<MessageInfo>
		implements MessageInfoDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<MessageInfoVo> getMessageInfoWithHQL(String sql,int startNum,int pageSize,Session session) {
		ArrayList<MessageInfoVo> arrayList = new ArrayList<MessageInfoVo>();
		try {
			// m.messageTitle,m.messageContext,m.messageDate,m.messageUpDown,m.messageCommentCount,u.userName,u.userSex,u.userAge,u.userPhoto,u.userJb 
			Query query = session.createSQLQuery(sql);
			query.setFirstResult(startNum);
			query.setMaxResults(pageSize);
			List<Object[]> data=query.list();
			for (Object[] objects : data) {
				MessageInfoVo vo=new MessageInfoVo();
				vo.setMessageTitle(String.valueOf(objects[0]));
				vo.setMessageContext(String.valueOf(objects[1]));
				vo.setMessageDate((Date)objects[2]);
				vo.setMessageUpAndDown(String.valueOf(objects[3]));
				vo.setMessageCommentCount(String.valueOf(objects[4]));
				vo.setUserName(String.valueOf(objects[5]));
				vo.setUserSex(String.valueOf(objects[6]));
				vo.setUserAge(String.valueOf(objects[7]));
				vo.setUserPhoto(String.valueOf(objects[8]));
				vo.setUserJb(String.valueOf(objects[9]));
				arrayList.add(vo);
			}
			return arrayList;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
		}
	}

	@Override
	public boolean addMessage(MessageInfo messageInfo,Session session) {
		return insertEntity(messageInfo, session);
	}

	@Override
	public boolean deleteMessage(MessageInfo messageInfo, Session session) {
		return deleteEntity(messageInfo, session);
	}

	@Override
	public boolean updateMessage(MessageInfo messageInfo, Session session) {
		return updateEntity(messageInfo, session);
	}

	@Override
	public MessageInfo findMessageWithMessageId(int messageId, Session session) {
		String hql="from MessageInfo where id="+messageId;
		return getObjectWithHql(hql, session);
	}
}
